/*!
 Temelia - Tree algorithms performance tests.

 Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

extern "C"
{
#include <temelia/tree_algorithms.h>
}

static volatile int __type;

static void null_handler(void *key, void *context)
{

}

static tree_t __temelia_tree(int type, int N)
{
	int k = 0;
	tree_t tree, root;

	tree = root = tree_new(int_new(k++), type);

	for (int i = 0; i < N / 2; i++)
	{
		for (int j = 0; j < i + 2 && k < N; j++)
			tree_add_child(tree, tree_new(default_collection[k++], type));
		tree = tree_get_child(tree, 0);
		if (k >= N)
			break;
	}

	return root;
}

static void temelia_tree_algorithms_dfs(int n)
{
	tree_t root = __temelia_tree(__type, n);
	tree_dfs_iterate(root, null_handler, stdout);
	tree_delete(root);
}

static void temelia_tree_algorithms_bfs(int n)
{
	tree_t root = __temelia_tree(__type, n);
	tree_bfs_iterate(root, null_handler, stdout);
	tree_delete(root);
}

void TemeliaPerformance::pt_tree_algorithms(void)
{
	TestUnit testUnit("tree_algorithms");

	// vector_t backbone
	__type = 0;
	GENERATE_TEMELIA_TEST(testUnit, "dfs_vector_t", tree_algorithms_dfs);
	GENERATE_TEMELIA_TEST(testUnit, "bfs_vector_t", tree_algorithms_bfs);

	// linked_list_t backbone
	__type = 1;
	GENERATE_TEMELIA_TEST(testUnit, "dfs_linked_list_t", tree_algorithms_dfs);
	GENERATE_TEMELIA_TEST(testUnit, "bfs_linked_list_t", tree_algorithms_bfs);

	testUnit.write(fout);
}
